<?php 
include_once("inc/connection.php");
include_once("inc/constants.inc.php");
session_start();

$student_id = $_SESSION['userID'];
//$aptitudeTestOver = false;
//$technicalTestOver = false;
$submitted='';
$testOver='';
if(!isset($_SESSION['login']) || empty($_SESSION['login']) || !isset($_SESSION['email']) || empty($_SESSION['email']))
{
	header("Location: login.php?error=session-timeout");
}
if(isset($_GET['test_type']) && !empty($_GET['test_type']))
{
	$test_type = $_GET['test_type'];
}
else
	header("Location: test.php?error=invalid-access");
	
	$res = mysql_query("SELECT * FROM `answer_sheet` where `studentID` = $_SESSION[userID] and `isReset`='no' "); 
	while($row = mysql_fetch_array($res))
	{
		$res1 = mysql_query("SELECT * FROM `question_type`"); 
		while($row1 = mysql_fetch_array($res1))
		{
			if(isset($row['typeId']) && $row['typeId']==$row1['QTypeid'])
			{
				header("Location: test.php?error=invalid-access");
				exit(0);
			}		
		}
	}

	// security
	$res = mysql_query("SELECT submitted FROM `login_details` where `userID` = $_SESSION[userID]");	
	while($row = mysql_fetch_array($res))
	{
		if($row['submitted']=="yes")
		$submitted = true;
	}
	$res = mysql_query("SELECT * FROM `answer_sheet` where `studentID` = $_SESSION[userID] and  `isReset`='no' ");	
	while($row = mysql_fetch_array($res))
	{
		$testOver = true;
	}
	if($submitted || $testOver)
	{
		header("Location: index.php?error=invalid-access");
		exit(0);
	}
	
	/*
	$res = mysql_query("SELECT * FROM `answer_sheet` where `studentID` = $_SESSION[userID] and  `isReset`='no' ");	
	while($row = mysql_fetch_array($res))
	{
		if(isset($row['typeId']) && $row['typeId']==1)
			$aptitudeTestOver = true;
		if(isset($row['typeId']) && $row['typeId']==2)
			$technicalTestOver = true;
	}
	
	if(($test_type == 1 && $aptitudeTestOver) || ($test_type == 2 && $technicalTestOver))
		header("Location: test.php?error=invalid-access");	
	*/	
	
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8"> 
		<title>Smartek online test | Test</title>
		<link rel="SHORTCUT ICON" href="img/disp/favicon.png" />	
		<link rel="stylesheet" href="css/bootstrap.css"  type="text/css"/>

	</head>
	<body>
		<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
		<script src="js/bootstrap.js"></script>
		<script src="js/jquery.timer.js"></script>
		<script src="js/timerfunction.js"></script>
		<script type="text/javascript">	
			function confirm_popup(){
				$('#confirm_popup').removeClass("hide"); 
				$("#confirm_popup").show();
				$("#confirm_popup").css('top',$(window).scrollTop()+200);
				$('#confirm_content').html('Do you really want to submit the answer sheet?');
				$('#confirm_no').click(function(){
						 $("#confirm_popup").hide();
						return false;
				});
				$('#confirm_yes').click(function(){
					$.get( "ajax/check-submit.php?typeId="+<?php echo $test_type; ?>+"&student_id="+<?php echo $student_id; ?>, function( check ) {
						if(check == 1){
							$("#confirm_popup").hide();
							$('#check-submit').removeClass("hide"); 
							alert("You have already submitted the answer sheet.");
							window.location = "logout.php";
							//$("#check-submit").show();
							//$("#check-submit").css('top',$(window).scrollTop()+200);
							//$('#check_content').html('You have already submitted the answer sheet.');
							$('#check_yes').click(function(){
								window.location = "logout.php";
							});							
						}
						else if(check == 2){
							document.getElementById("answer-sheet").submit();
						}
					});
				});   
			}
			
			$(function(){				 
				 $.get( "ajax/check-submit.php?typeId="+<?php echo $test_type; ?>+"&student_id="+<?php echo $student_id; ?>, function( check ) {
					if(check == 1){
						window.location = "logout.php?already-submitted";
						$('#check-submit').removeClass("hide"); 
						$("#check-submit").show();
						$("#check-submit").css('top',$(window).scrollTop()+200);
						$('#check_content').html('You have already submitted the answer sheet.');
						$('#check_yes').click(function(){
							window.location = "logout.php";
						});							
					}
				});
			});
			
			$(function(){				 
				 $.get( "ajax/get_exam_time.php?typeId="+<?php echo $test_type; ?>+"&student_id="+<?php echo $student_id; ?>, function( data ) {
					startTimer(data, "#answer-sheet");
				});
			});
			
			$(function(){
				 $("#showTimer").hide();
			});
			
			$(document).ready(function(){
				  $("#hideTimer").click(function(){
					$("#timer-content").hide();
					$("#hideTimer").hide();
					$("#showTimer").show();
				  });
				  $("#showTimer").click(function(){
					$("#timer-content").show();
					$("#showTimer").hide();
					$("#hideTimer").show();
				  });
			});
			
			window.location.hash="no-back-button";
			window.location.hash="Again-No-back-button";//again because google chrome don't insert first hash into history
			window.onhashchange=function(){window.location.hash="no-back-button";}
			
		</script>
		<div class="container">
			<img src="img/disp/logo.png" alt="">
		</div>
		
		
		<div class="container">
			<hr class="featurette-divider">
		</div>


		<div id="confirm_popup" class="hide">
			<div id="confirm_content"></div>
			<br/><br/>
			<input id='confirm_yes' type='button' class="btn btn-default" value='Yes' />
			<input id='confirm_no' type='button' class="btn btn-primary" value='No' />
		</div>		
		<div id="check-submit" class="hide">
			<div id="check_content"></div>
			<br/><br/>
			<input id='check_yes' type='button' class="btn btn-primary" value='Ok' />
		</div>	
		<div class="container">
			<div class="row">
				<div class="span12" > <!--pagination-centered-->
					<form method="post" id="answer-sheet" action="process/submit-answer.php">						
						<h4>Instructions:</h4>
						<?php 
						$counter = mysql_query("SELECT COUNT(*) AS id FROM instructions");
                        $num = mysql_fetch_array($counter);
                        $count = $num["id"];
						for($i=1;$i<=$count;$i++)
							{
								$result1 = mysql_query("SELECT * FROM instructions WHERE serial_no='$i' && showInstruction='y'");			
								while($row = mysql_fetch_array($result1))
									{
									echo "<b>" . $row['serial_no'].". ". $row['description']."</b></br>" ;
									}
							}
						?>
						<hr/>
						<div id="countdown-wrapper" >
							<div id="timer-content">
								Time remaining<br/><br/>
								&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="countdown"></span>
							</div>
							</br>
							<div class="pull-right">
								<input type="button" id="hideTimer" class="btn" value="Hide Timer"/>
								<input type="button" id="showTimer" class="btn" value="Show Timer"/>
							</div>							
						</div>
						<div class="container" style="position:fixed;">
							<div class="row pull-right">
								<div class="span4 pull-right" >
								</div>
							</div>
						</div>

						<?php if(isset($instruction)) echo $instruction; ?>
						</br></br>
						<?php
							$result = mysql_query("SELECT * FROM question_table WHERE typeId=$test_type order by rand() LIMIT 15");	
								$question_number = 0;
								while($row = mysql_fetch_array($result))
								{	
								$question_number++;
						?>
								<div class="container">
									<div class="row">
										<div class="span12">
										<?php echo "</br><b>".$question_number.". ".$row['question']."</b></br>"; ?>
										</div>
										<div class="span11 choices">
											<?php if(trim($row['optionOne'])!=""){ ?>
											<input type="radio" name="<?php echo $question_number; ?>"  value="<?php echo $question_number."_".$row['questionNumber']."_a"; ?>"> <b>A. </b> <?php echo $row['optionOne']."</br>";  }?>
											<?php if(trim($row['optionTwo'])!=""){ ?>
											<input type="radio" name="<?php echo $question_number; ?>"  value="<?php echo $question_number."_".$row['questionNumber']."_b"; ?>"> <b>B. </b> <?php echo $row['optionTwo']."</br>";  } ?>
											<?php if(trim($row['optionThree'])!=""){ ?>											
											<input type="radio" name="<?php echo $question_number; ?>"  value="<?php echo $question_number."_".$row['questionNumber']."_c"; ?>"> <b>C. </b> <?php echo $row['optionThree']."</br>"; } ?>
											<?php if(trim($row['optionFour'])!=""){ ?>
											<input type="radio" name="<?php echo $question_number; ?>"  value="<?php echo $question_number."_".$row['questionNumber']."_d"; ?>"> <b>D. </b> <?php echo $row['optionFour']."</br>";  }?>
										</div>										
									</div>
								</div>	
						<?php		
								}
								
						?>	
						</br></br>
						<input type="hidden" name="duration" value="60" />
						<input type="hidden" name="test_type" value="<?php echo $test_type; ?>" />
					   <input type="button" class="btn" name="button" value="Submit" onclick="confirm_popup()" />
					</form>
				</div>
			</div>
		</div>
		</br>
		<div class="container">
			<hr class="featurette-divider">
		</div>
		<div class="container marketing">
      <!-- FOOTER -->
      <footer>
        <p>&copy; 2014 SCS &middot; </p>
      </footer>

    </div><!-- /.container -->
	</body>
</html>